package com.cssy.xxkj.basic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.constants.Constants;
import com.cssy.xxkj.basic.dao.BasicCustomerContactDAO;
import com.cssy.xxkj.basic.entity.BasicCompanyTestDO;
import com.cssy.xxkj.basic.entity.BasicContactRecordDO;
import com.cssy.xxkj.basic.entity.BasicCustomerContactDO;
import com.cssy.xxkj.basic.service.IBasicCompanyTestService;
import com.cssy.xxkj.basic.service.IBasicContactRecordService;
import com.cssy.xxkj.basic.service.IBasicCustomerContactService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Service("basicCustomerContactService")
public class BasicCustomerContactServiceImpl  implements IBasicCustomerContactService {

	@Resource(name="basicCustomerContactDAO")
	private BasicCustomerContactDAO basicCustomerContactDAO;

	@Resource(name = "basicCompanyTestService")
	private IBasicCompanyTestService basicCompanyTestService;

	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name="basicContactRecordService")
	private IBasicContactRecordService basicContactRecordService;

	@Override
	@Transactional
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicCustomerContactDO>  list=basicCustomerContactDAO.find(scs);
		rs.setRecordCount(basicCustomerContactDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return basicCustomerContactDAO.deleteBasicCustomerContactDOByPrimaryKey(id);
	}

	@Override
	public Integer update(BasicCustomerContactDO t, User user)
			throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return basicCustomerContactDAO.updateBasicCustomerContactDO(t);
	}

	@Override
	public BasicCustomerContactDO save(BasicCustomerContactDO t, User user)
			throws SyBaseException {
		String id=IDCodeTypeUtil.getId();
		t.setId(id);
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(new Date());
		t.setBizVersion(1);
		t.setBizModifyName(user.getName());
		basicCustomerContactDAO.insertBasicCustomerContactDO(t);
		return t;
	}

	@Override
	public BasicCustomerContactDO findListById(String id)
			throws SyBaseException {
		return basicCustomerContactDAO.findBasicCustomerContactDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return basicCustomerContactDAO.deleteBizRemoved(id);
	}

	public BodyMsg  saveOrUpdate(HttpServletRequest request,BasicCompanyTestDO curAP)throws SyBaseException{
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String itemdata = request.getParameter("newVal");//更新后的数据
			
			String recorddata=request.getParameter("recordData");//联系人数据

			List<BasicCustomerContactDO> list=JSONArray.parseArray(itemdata,BasicCustomerContactDO.class);
			
			List<BasicContactRecordDO> recordList=JSONArray.parseArray(recorddata,BasicContactRecordDO.class);
			
			BasicCompanyTestDO cdo=new BasicCompanyTestDO();
			if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改 主表数据的跟新
				basicCompanyTestService.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(curAP.getId());
				cdo=curAP;
			} else {// 新增
				String id=IDCodeTypeUtil.getId();
				curAP.setId(id);
				curAP.setZd001Code(syCodeService.getCode("CP", "CP", SyCode.DATE_YYMMDD, 4));
				curAP.setZd032Type(Constants.COMPANY_TYPE_CUST);
				cdo = basicCompanyTestService.save(curAP,SessionUtil.getUser(request));
				cdo.setId(id);
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(cdo.getId());
			}
			//由于不分行所以删除原先的所有的数据
			basicCustomerContactDAO.deleteAllByCustId(cdo.getId());
			if(list!=null&&list.size()>0&&!list.isEmpty()){  //修改判定
				for(BasicCustomerContactDO bd:list){//客户联系人
						bd.setZd001CustId(cdo.getId());
						bd.setZd001CustCode(cdo.getZd001Code());
						bd.setZd001CustName(cdo.getZd003ZhName());
						this.save(bd, SessionUtil.getUser(request));
				}
			}
			//由于不分行所以删除原先的所有的数据
			basicContactRecordService.deleteAllByCustId(cdo.getId());
			if(recordList!=null&&recordList.size()>0&&!recordList.isEmpty()){
				for(BasicContactRecordDO bcrd:recordList){
					basicContactRecordService.
						saveBasicContactRecordDOByBasicCompanyTestDO(cdo, bcrd, SessionUtil.getUser(request));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		
		return bodyMap;

	}
}
